/* GNUPLOT - bitmap.c */

/*[
 * Copyright Jyrki Yli-Nokari 1987
 *
 * Gnuplot license:
 *
 * Permission to use, copy, and distribute this software and its
 * documentation for any purpose with or without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.
 *
 * Permission to modify the software is granted, but not the right to
 * distribute the complete modified source code.  Modifications are to
 * be distributed as patches to the released version.  Permission to
 * distribute binaries produced by compiling modified sources is granted,
 * provided you
 *   1. distribute the corresponding source modifications from the
 *    released version in the form of a patch file along with the binaries,
 *   2. add special version identification to distinguish your version
 *    in addition to the base release version number,
 *   3. provide your name and address as the primary contact for the
 *    support of your modified version, and
 *   4. retain our contact information in regard to use of the base
 *    software.
 * Permission to distribute the released version of the source code along
 * with corresponding source modifications in the form of a patch file is
 * granted with same provisions 2 through 4 for binary distributions.
 *
 * This software is provided "as is" without express or implied warranty
 * to the extent permitted by applicable law.
 *
 * Alternative license:
 *
 * As an alternative to distributing code in this file under the gnuplot license,
 * you may instead comply with the terms below. In this case, redistribution and
 * use in source and binary forms, with or without modification, are permitted
 * provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
]*/

/*
 * AUTHORS
 *
 *   Original Software:
 *     Jyrki Yli-Nokari <jty@intrin.UUCP> (1987)
 *   Modified for use with gnuplot (1990):
 *     Ronald J. Hartranft <rjh2@ns.cc.lehigh.edu>
 *     Russell Lang <rjl@monu1.cc.monash.edu.au>
 */

/*
   ** Jyrki Yli-Nokari, December 2013.
   ** Change to dual licence (Gnuplot + BSD) and remove previous
   ** restriction to noncommercial use.
 */

#include "bitmap.h"

#include "alloc.h"
#include "util.h"
#include "term_api.h"	/* EAM - to pick up fillstyle definitions */

/* global variables */
bitmap *b_p = (bitmap *) NULL;	/* global pointer to bitmap */

unsigned int b_xsize, b_ysize;	/* the size of the bitmap */
unsigned int b_planes;		/* number of color planes */
unsigned int b_psize;		/* size of each plane */
unsigned int b_rastermode;	/* raster mode rotates -90deg */
unsigned int b_linemask = 0xffff; /* 16 bit mask for dotted lines */
         int b_angle;		/* rotation of text */
enum JUSTIFY b_justify = LEFT;	/* text justification */

int b_maskcount = 0;

/* Local prototypes */
static GP_INLINE void b_setpixel(unsigned int x, unsigned int y, unsigned int value);
static GP_INLINE void b_setmaskpixel(unsigned int x, unsigned int y, unsigned int value);
static void b_line(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2);
static void b_wline(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2);

/* file-scope variables */

static unsigned int b_value = 1; /* colour of lines */
static double       b_lw = 1;	/* line width */
static unsigned int b_currx, b_curry; /* the current coordinates */
static unsigned int b_hchar;	/* width of characters */
static unsigned int b_hbits;	/* actual bits in char horizontally */
static unsigned int b_vchar;	/* height of characters */
static unsigned int b_vbits;	/* actual bits in char vertically */
static char_box b_font[FNT_CHARS]; /* the current font */
static unsigned int b_pattern[] = { 0xffff, 0x1111, 0xffff, 0x5555,
				    0x3333, 0x7777, 0x3f3f, 0x0f0f, 0x5f5f };
static unsigned int b_lastx;
static unsigned int b_lasty;	/* last pixel set - used by b_line */


/* 5x9 font, bottom row first, left pixel in lsb */
const char_row fnt5x9[FNT_CHARS][FNT5X9_VBITS] = {
  /* */  {000000,000000,000000,000000,000000,000000,000000,000000,000000},
  /*!*/  {000000,000000,0x0004,000000,0x0004,0x0004,0x0004,0x0004,0x0004},
  /*"*/  {000000,000000,000000,000000,000000,000000,0x000a,0x000a,0x000a},
  /*#*/  {000000,000000,0x000a,0x000a,0x001f,0x000a,0x001f,0x000a,0x000a},
  /*$*/  {000000,000000,0x0004,0x000f,0x0014,0x000e,0x0005,0x001e,0x0004},
  /*%*/  {000000,000000,0x0018,0x0019,0x0002,0x0004,0x0008,0x0013,0x0003},
  /*&*/  {000000,000000,0x0016,0x0009,0x0015,0x0002,0x0005,0x0005,0x0002},
  /*'*/  {000000,000000,000000,000000,000000,0x0002,0x0004,0x0006,0x0006},
  /*(*/  {000000,000000,0x0008,0x0004,0x0002,0x0002,0x0002,0x0004,0x0008},
  /*)*/  {000000,000000,0x0002,0x0004,0x0008,0x0008,0x0008,0x0004,0x0002},
  /***/  {000000,000000,0x0004,0x0015,0x000e,0x001f,0x000e,0x0015,0x0004},
  /*+*/  {000000,000000,000000,0x0004,0x0004,0x001f,0x0004,0x0004,000000},
  /*,*/  {000000,0x0002,0x0004,0x0006,0x0006,000000,000000,000000,000000},
  /*-*/  {000000,000000,000000,000000,000000,0x001f,000000,000000,000000},
  /*.*/  {000000,000000,0x0006,0x0006,000000,000000,000000,000000,000000},
  /*-/-*/{000000,000000,000000,0x0001,0x0002,0x0004,0x0008,0x0010,000000},
  /*0*/  {000000,000000,0x000e,0x0011,0x0013,0x0015,0x0019,0x0011,0x000e},
  /*1*/  {000000,000000,0x000e,0x0004,0x0004,0x0004,0x0004,0x0006,0x0004},
  /*2*/  {000000,000000,0x001f,0x0001,0x0001,0x000e,0x0010,0x0011,0x000e},
  /*3*/  {000000,000000,0x000e,0x0011,0x0010,0x000c,0x0010,0x0011,0x000e},
  /*4*/  {000000,000000,0x0008,0x0008,0x001f,0x0009,0x000a,0x000c,0x0008},
  /*5*/  {000000,000000,0x000e,0x0011,0x0010,0x0010,0x000f,0x0001,0x001f},
  /*6*/  {000000,000000,0x000e,0x0011,0x0011,0x000f,0x0001,0x0002,0x000c},
  /*7*/  {000000,000000,0x0001,0x0001,0x0002,0x0004,0x0008,0x0010,0x001f},
  /*8*/  {000000,000000,0x000e,0x0011,0x0011,0x000e,0x0011,0x0011,0x000e},
  /*9*/  {000000,000000,0x0006,0x0008,0x0010,0x001e,0x0011,0x0011,0x000e},
  /*:*/  {000000,000000,000000,0x0006,0x0006,000000,0x0006,0x0006,000000},
  /*;*/  {000000,0x0001,0x0002,0x0006,0x0006,000000,0x0006,0x0006,000000},
  /*<*/  {000000,000000,0x0008,0x0004,0x0002,0x0001,0x0002,0x0004,0x0008},
  /*=*/  {000000,000000,000000,000000,0x001f,000000,0x001f,000000,000000},
  /*>*/  {000000,000000,0x0002,0x0004,0x0008,0x0010,0x0008,0x0004,0x0002},
  /*?*/  {000000,000000,0x0004,000000,0x0004,0x0008,0x0010,0x0011,0x000e},
  /*@*/  {000000,000000,0x000e,0x0015,0x0015,0x0016,0x0010,0x0011,0x000e},
  /*A*/  {000000,000000,0x0011,0x0011,0x001f,0x0011,0x0011,0x000a,0x0004},
  /*B*/  {000000,000000,0x000f,0x0012,0x0012,0x000e,0x0012,0x0012,0x000f},
  /*C*/  {000000,000000,0x000e,0x0011,0x0001,0x0001,0x0001,0x0011,0x000e},
  /*D*/  {000000,000000,0x000f,0x0012,0x0012,0x0012,0x0012,0x0012,0x000f},
  /*E*/  {000000,000000,0x001f,0x0001,0x0001,0x0007,0x0001,0x0001,0x001f},
  /*F*/  {000000,000000,0x0001,0x0001,0x0001,0x0007,0x0001,0x0001,0x001f},
  /*G*/  {000000,000000,0x001e,0x0011,0x0011,0x0019,0x0001,0x0001,0x001e},
  /*H*/  {000000,000000,0x0011,0x0011,0x0011,0x001f,0x0011,0x0011,0x0011},
  /*I*/  {000000,000000,0x000e,0x0004,0x0004,0x0004,0x0004,0x0004,0x000e},
  /*J*/  {000000,000000,0x000e,0x0011,0x0010,0x0010,0x0010,0x0010,0x0010},
  /*K*/  {000000,000000,0x0011,0x0009,0x0005,0x0003,0x0005,0x0009,0x0011},
  /*L*/  {000000,000000,0x001f,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001},
  /*M*/  {000000,000000,0x0011,0x0011,0x0011,0x0015,0x0015,0x001b,0x0011},
  /*N*/  {000000,000000,0x0011,0x0011,0x0011,0x0019,0x0015,0x0013,0x0011},
  /*O*/  {000000,000000,0x000e,0x0011,0x0011,0x0011,0x0011,0x0011,0x000e},
  /*P*/  {000000,000000,0x0001,0x0001,0x0001,0x000f,0x0011,0x0011,0x000f},
  /*Q*/  {000000,0x0018,0x000e,0x0015,0x0011,0x0011,0x0011,0x0011,0x000e},
  /*R*/  {000000,000000,0x0011,0x0009,0x0005,0x000f,0x0011,0x0011,0x000f},
  /*S*/  {000000,000000,0x000e,0x0011,0x0010,0x000e,0x0001,0x0011,0x000e},
  /*T*/  {000000,000000,0x0004,0x0004,0x0004,0x0004,0x0004,0x0004,0x001f},
  /*U*/  {000000,000000,0x000e,0x0011,0x0011,0x0011,0x0011,0x0011,0x0011},
  /*V*/  {000000,000000,0x0004,0x0004,0x000a,0x000a,0x0011,0x0011,0x0011},
  /*W*/  {000000,000000,0x0011,0x001b,0x0015,0x0011,0x0011,0x0011,0x0011},
  /*X*/  {000000,000000,0x0011,0x0011,0x000a,0x0004,0x000a,0x0011,0x0011},
  /*Y*/  {000000,000000,0x0004,0x0004,0x0004,0x0004,0x000a,0x0011,0x0011},
  /*Z*/  {000000,000000,0x001f,0x0001,0x0002,0x0004,0x0008,0x0010,0x001f},
  /*[*/  {000000,000000,0x000e,0x0002,0x0002,0x0002,0x0002,0x0002,0x000e},
  /*\ */ {000000,000000,000000,0x0010,0x0008,0x0004,0x0002,0x0001,000000},
  /*]*/  {000000,000000,0x000e,0x0008,0x0008,0x0008,0x0008,0x0008,0x000e},
  /*^*/  {000000,000000,000000,000000,000000,000000,0x0011,0x000a,0x0004},
  /*_*/  {000000,000000,0x001f,000000,000000,000000,000000,000000,000000},
  /*`*/  {000000,000000,000000,000000,000000,0x0008,0x0004,0x000c,0x000c},
  /*a*/  {000000,000000,0x001e,0x0011,0x001e,0x0010,0x000e,000000,000000},
  /*b*/  {000000,000000,0x000d,0x0013,0x0011,0x0013,0x000d,0x0001,0x0001},
  /*c*/  {000000,000000,0x000e,0x0011,0x0001,0x0011,0x000e,000000,000000},
  /*d*/  {000000,000000,0x0016,0x0019,0x0011,0x0019,0x0016,0x0010,0x0010},
  /*e*/  {000000,000000,0x000e,0x0001,0x001f,0x0011,0x000e,000000,000000},
  /*f*/  {000000,000000,0x0004,0x0004,0x0004,0x000e,0x0004,0x0014,0x0008},
  /*g*/  {0x000e,0x0011,0x0016,0x0019,0x0011,0x0019,0x0016,000000,000000},
  /*h*/  {000000,000000,0x0011,0x0011,0x0011,0x0013,0x000d,0x0001,0x0001},
  /*i*/  {000000,000000,0x000e,0x0004,0x0004,0x0004,0x0006,000000,0x0004},
  /*j*/  {0x0006,0x0009,0x0008,0x0008,0x0008,0x0008,0x000c,000000,0x0008},
  /*k*/  {000000,000000,0x0009,0x0005,0x0003,0x0005,0x0009,0x0001,0x0001},
  /*l*/  {000000,000000,0x000e,0x0004,0x0004,0x0004,0x0004,0x0004,0x0006},
  /*m*/  {000000,000000,0x0015,0x0015,0x0015,0x0015,0x000b,000000,000000},
  /*n*/  {000000,000000,0x0011,0x0011,0x0011,0x0013,0x000d,000000,000000},
  /*o*/  {000000,000000,0x000e,0x0011,0x0011,0x0011,0x000e,000000,000000},
  /*p*/  {0x0001,0x0001,0x000d,0x0013,0x0011,0x0013,0x000d,000000,000000},
  /*q*/  {0x0010,0x0010,0x0016,0x0019,0x0011,0x0019,0x0016,000000,000000},
  /*r*/  {000000,000000,0x0001,0x0001,0x0001,0x0013,0x000d,000000,000000},
  /*s*/  {000000,000000,0x000f,0x0010,0x000e,0x0001,0x001e,000000,000000},
  /*t*/  {000000,000000,0x0008,0x0014,0x0004,0x0004,0x001f,0x0004,0x0004},
  /*u*/  {000000,000000,0x0016,0x0019,0x0011,0x0011,0x0011,000000,000000},
  /*v*/  {000000,000000,0x0004,0x000a,0x0011,0x0011,0x0011,000000,000000},
  /*w*/  {000000,000000,0x000a,0x0015,0x0015,0x0011,0x0011,000000,000000},
  /*x*/  {000000,000000,0x0011,0x000a,0x0004,0x000a,0x0011,000000,000000},
  /*y*/  {0x000e,0x0010,0x001e,0x0011,0x0011,0x0011,0x0011,000000,000000},
  /*z*/  {000000,000000,0x001f,0x0002,0x0004,0x0008,0x001f,000000,000000},
  /*{*/  {000000,000000,0x0008,0x0004,0x0004,0x0002,0x0004,0x0004,0x0008},
  /*|*/  {000000,000000,0x0004,0x0004,0x0004,000000,0x0004,0x0004,0x0004},
  /*}*/  {000000,000000,0x0002,0x0004,0x0004,0x0008,0x0004,0x0004,0x0002},
  /*~*/  {000000,000000,000000,000000,000000,000000,0x0008,0x0015,0x0002},
  /*DEL*/{000000,000000,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f},
};

/* 9x17 font, bottom row first, left pixel in lsb */
const char_row fnt9x17[FNT_CHARS][FNT9X17_VBITS] = {
  /* */  {000000,000000,000000,000000,000000,000000,000000,000000,000000,
          000000,000000,000000,000000,000000,000000,000000,000000},
  /*!*/  {000000,000000,000000,000000,0x0010,000000,000000,000000,0x0010,
          0x0010,0x0010,0x0010,0x0010,0x0010,0x0010,0x0010,0x0010},
  /*"*/  {000000,000000,000000,000000,000000,000000,000000,000000,000000,
          000000,000000,000000,0x0044,0x0044,0x0044,0x0044,0x0044},
  /*#*/  {000000,000000,000000,000000,0x0044,0x0044,0x0044,0x0044,0x01ff,
          0x0044,0x0044,0x0044,0x01ff,0x0044,0x0044,0x0044,0x0044},
  /*$*/  {000000,000000,000000,000000,0x0010,0x0010,0x007e,0x0091,0x0110,
          0x0090,0x007c,0x0012,0x0011,0x0112,0x00fc,0x0010,0x0010},
  /*%*/  {000000,000000,000000,000000,0x0080,0x0141,0x0081,0x0002,0x0004,
          0x0008,0x0010,0x0020,0x0040,0x0080,0x0102,0x0105,0x0002},
  /*&*/  {000000,000000,000000,000000,0x011c,0x00a2,0x0041,0x00c1,0x0141,
          0x0022,0x001c,0x0014,0x0022,0x0022,0x001c,000000,000000},
  /*'*/  {000000,000000,000000,000000,000000,000000,000000,000000,000000,
          000000,000000,0x0004,0x0008,0x0010,0x0030,0x0038,0x0010},
  /*(*/  {000000,000000,000000,000000,0x0040,0x0020,0x0010,0x0008,0x0008,
          0x0004,0x0004,0x0004,0x0008,0x0008,0x0010,0x0020,0x0040},
  /*)*/  {000000,000000,000000,000000,0x0004,0x0008,0x0010,0x0020,0x0020,
          0x0040,0x0040,0x0040,0x0020,0x0020,0x0010,0x0008,0x0004},
  /***/  {000000,000000,000000,000000,0x0010,0x0010,0x0111,0x0092,0x0054,
          0x0038,0x01ff,0x0038,0x0054,0x0092,0x0111,0x0010,0x0010},
  /*+*/  {000000,000000,000000,000000,000000,000000,0x0010,0x0010,0x0010,
          0x0010,0x01ff,0x0010,0x0010,0x0010,0x0010,000000,000000},
  /*,*/  {000000,000000,0x0004,0x0008,0x0010,0x0030,0x0038,0x0010,000000,
          000000,000000,000000,000000,000000,000000,000000,000000},
  /*-*/  {000000,000000,000000,000000,000000,000000,000000,000000,000000,
          000000,0x01ff,000000,000000,000000,000000,000000,000000},
  /*.*/  {000000,000000,000000,000000,0x0010,0x0038,0x0010,000000,000000,
          000000,000000,000000,000000,000000,000000,000000,000000},
  /*-/-*/{000000,000000,000000,000000,000000,000000,0x0001,0x0002,0x0004,
          0x0008,0x0010,0x0020,0x0040,0x0080,0x0100,000000,000000},
  /*0*/  {000000,000000,000000,000000,0x007c,0x0082,0x0101,0x0103,0x0105,
          0x0109,0x0111,0x0121,0x0141,0x0181,0x0101,0x0082,0x007c},
  /*1*/  {000000,000000,000000,000000,0x007c,0x0010,0x0010,0x0010,0x0010,
          0x0010,0x0010,0x0010,0x0010,0x0010,0x001c,0x0018,0x0010},
  /*2*/  {000000,000000,000000,000000,0x01ff,0x0001,0x0001,0x0001,0x0001,
          0x0002,0x007c,0x0080,0x0100,0x0100,0x0101,0x0082,0x007c},
  /*3*/  {000000,000000,000000,000000,0x007c,0x0082,0x0101,0x0100,0x0100,
          0x0080,0x0078,0x0080,0x0100,0x0100,0x0101,0x0082,0x007c},
  /*4*/  {000000,000000,000000,000000,0x0040,0x0040,0x0040,0x0040,0x0040,
          0x01ff,0x0041,0x0042,0x0044,0x0048,0x0050,0x0060,0x0040},
  /*5*/  {000000,000000,000000,000000,0x007c,0x0082,0x0101,0x0100,0x0100,
          0x0100,0x0080,0x007f,0x0001,0x0001,0x0001,0x0001,0x01ff},
  /*6*/  {000000,000000,000000,000000,0x007c,0x0082,0x0101,0x0101,0x0101,
          0x0081,0x007f,0x0001,0x0001,0x0001,0x0002,0x0084,0x0078},
  /*7*/  {000000,000000,000000,000000,0x0001,0x0001,0x0001,0x0002,0x0004,
          0x0008,0x0010,0x0020,0x0040,0x0080,0x0100,0x0100,0x01ff},
  /*8*/  {000000,000000,000000,000000,0x007c,0x0082,0x0101,0x0101,0x0101,
          0x0082,0x007c,0x0082,0x0101,0x0101,0x0101,0x0082,0x007c},
  /*9*/  {000000,000000,000000,000000,0x001c,0x0022,0x0040,0x0080,0x0100,
          0x0100,0x01fc,0x0102,0x0101,0x0101,0x0101,0x0082,0x007c},
  /*:*/  {000000,000000,000000,000000,000000,000000,0x0010,0x0038,0x0010,
          000000,000000,000000,0x0010,0x0038,0x0010,000000,000000},
  /*;*/  {000000,000000,000000,0x0004,0x0008,0x0010,0x0030,0x0038,0x0010,
          000000,000000,000000,0x0010,0x0038,0x0010,000000,000000},
  /*<*/  {000000,000000,000000,000000,0x0040,0x0020,0x0010,0x0008,0x0004,
          0x0002,0x0001,0x0002,0x0004,0x0008,0x0010,0x0020,0x0040},
  /*=*/  {000000,000000,000000,000000,000000,000000,000000,000000,0x01ff,
          000000,000000,000000,0x01ff,000000,000000,000000,000000},
  /*>*/  {000000,000000,000000,000000,0x0004,0x0008,0x0010,0x0020,0x0040,
          0x0080,0x0100,0x0080,0x0040,0x0020,0x0010,0x0008,0x0004},
  /*?*/  {000000,000000,000000,0x0010,0x0038,0x0010,000000,0x0010,0x0010,
          0x0020,0x0040,0x0080,0x0100,0x0100,0x0101,0x0082,0x007c},
  /*@*/  {000000,000000,000000,000000,0x007c,0x0002,0x0001,0x01f9,0x0145,
          0x0145,0x0145,0x0179,0x0101,0x0101,0x0101,0x0082,0x007c},
  /*A*/  {000000,000000,000000,000000,0x0101,0x0101,0x0101,0x0101,0x01ff,
          0x0101,0x0082,0x0082,0x0044,0x0044,0x0028,0x0028,0x0010},
  /*B*/  {000000,000000,000000,000000,0x007f,0x0084,0x0104,0x0104,0x0104,
          0x0084,0x007c,0x0084,0x0104,0x0104,0x0104,0x0084,0x007f},
  /*C*/  {000000,000000,000000,000000,0x007c,0x0082,0x0101,0x0001,0x0001,
          0x0001,0x0001,0x0001,0x0001,0x0001,0x0101,0x0082,0x007c},
  /*D*/  {000000,000000,000000,000000,0x007f,0x0084,0x0104,0x0104,0x0104,
          0x0104,0x0104,0x0104,0x0104,0x0104,0x0104,0x0084,0x007f},
  /*E*/  {000000,000000,000000,000000,0x01ff,0x0001,0x0001,0x0001,0x0001,
          0x0001,0x001f,0x0001,0x0001,0x0001,0x0001,0x0001,0x01ff},
  /*F*/  {000000,000000,000000,000000,0x0001,0x0001,0x0001,0x0001,0x0001,
          0x0001,0x001f,0x0001,0x0001,0x0001,0x0001,0x0001,0x01ff},
  /*G*/  {000000,000000,000000,000000,0x00fc,0x0102,0x0101,0x0101,0x0101,
          0x0101,0x01c1,0x0001,0x0001,0x0001,0x0001,0x0102,0x00fc},
  /*H*/  {000000,000000,000000,000000,0x0101,0x0101,0x0101,0x0101,0x0101,
          0x0101,0x01ff,0x0101,0x0101,0x0101,0x0101,0x0101,0x0101},
  /*I*/  {000000,000000,000000,000000,0x007c,0x0010,0x0010,0x0010,0x0010,
          0x0010,0x0010,0x0010,0x0010,0x0010,0x0010,0x0010,0x007c},
  /*J*/  {000000,000000,000000,000000,0x007c,0x0082,0x0101,0x0100,0x0100,
          0x0100,0x0100,0x0100,0x0100,0x0100,0x0100,0x0100,0x0180},
  /*K*/  {000000,000000,000000,000000,0x0101,0x0081,0x0041,0x0021,0x0011,
          0x0009,0x0005,0x000b,0x0011,0x0021,0x0041,0x0081,0x0101},
  /*L*/  {000000,000000,000000,000000,0x01ff,0x0101,0x0001,0x0001,0x0001,
          0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001},
  /*M*/  {000000,000000,000000,000000,0x0101,0x0101,0x0101,0x0101,0x0101,
          0x0101,0x0111,0x0111,0x0129,0x0145,0x0145,0x0183,0x0101},
  /*N*/  {000000,000000,000000,000000,0x0101,0x0101,0x0101,0x0181,0x0141,
          0x0141,0x0121,0x0111,0x0109,0x0105,0x0105,0x0103,0x0101},
  /*O*/  {000000,000000,000000,000000,0x007c,0x0082,0x0101,0x0101,0x0101,
          0x0101,0x0101,0x0101,0x0101,0x0101,0x0101,0x0082,0x007c},
  /*P*/  {000000,000000,000000,000000,0x0001,0x0001,0x0001,0x0001,0x0001,
          0x0001,0x007f,0x0081,0x0101,0x0101,0x0101,0x0081,0x007f},
  /*Q*/  {000000,000000,0x0180,0x0040,0x007c,0x0092,0x0101,0x0101,0x0101,
          0x0101,0x0101,0x0101,0x0101,0x0101,0x0101,0x0082,0x007c},
  /*R*/  {000000,000000,000000,000000,0x0101,0x0081,0x0041,0x0021,0x0011,
          0x0009,0x007f,0x0081,0x0101,0x0101,0x0101,0x0081,0x007f},
  /*S*/  {000000,000000,000000,000000,0x007c,0x0082,0x0101,0x0100,0x0100,
          0x0080,0x007c,0x0002,0x0001,0x0001,0x0101,0x0082,0x007c},
  /*T*/  {000000,000000,000000,000000,0x0038,0x0010,0x0010,0x0010,0x0010,
          0x0010,0x0010,0x0010,0x0010,0x0010,0x0010,0x0111,0x01ff},
  /*U*/  {000000,000000,000000,000000,0x007c,0x0082,0x0101,0x0101,0x0101,
          0x0101,0x0101,0x0101,0x0101,0x0101,0x0101,0x0101,0x0101},
  /*V*/  {000000,000000,000000,000000,0x0010,0x0010,0x0028,0x0028,0x0044,
          0x0044,0x0082,0x0082,0x0101,0x0101,0x0101,0x0101,0x0101},
  /*W*/  {000000,000000,000000,000000,0x0101,0x0183,0x0145,0x0145,0x0129,
          0x0111,0x0111,0x0101,0x0101,0x0101,0x0101,0x0101,0x0101},
  /*X*/  {000000,000000,000000,000000,0x0101,0x0101,0x0082,0x0082,0x0044,
          0x0028,0x0010,0x0028,0x0044,0x0082,0x0082,0x0101,0x0101},
  /*Y*/  {000000,000000,000000,000000,0x0010,0x0010,0x0010,0x0010,0x0010,
          0x0010,0x0010,0x0028,0x0044,0x0082,0x0082,0x0101,0x0101},
  /*Z*/  {000000,000000,000000,000000,0x01ff,0x0001,0x0002,0x0002,0x0004,
          0x0008,0x0010,0x0020,0x0040,0x0080,0x0080,0x0100,0x01ff},
  /*[*/  {000000,000000,000000,000000,0x007c,0x0004,0x0004,0x0004,0x0004,
          0x0004,0x0004,0x0004,0x0004,0x0004,0x0004,0x0004,0x007c},
  /*\ */ {000000,000000,000000,000000,000000,000000,0x0100,0x0080,0x0040,
          0x0020,0x0010,0x0008,0x0004,0x0002,0x0001,000000,000000},
  /*]*/  {000000,000000,000000,000000,0x007c,0x0040,0x0040,0x0040,0x0040,
          0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x007c},
  /*^*/  {000000,000000,000000,000000,000000,000000,000000,000000,000000,
          000000,000000,000000,0x0101,0x0082,0x0044,0x0028,0x0010},
  /*_*/  {000000,000000,000000,000000,0x01ff,000000,000000,000000,000000,
          000000,000000,000000,000000,000000,000000,000000,000000},
  /*`*/  {000000,000000,000000,000000,000000,000000,000000,000000,000000,
          000000,000000,0x0020,0x0010,0x0008,0x000c,0x001c,0x0008},
  /*a*/  {000000,000000,000000,000000,0x03fc,0x0102,0x0101,0x0102,0x01fc,
          0x0100,0x0100,0x0080,0x007c,000000,000000,000000,000000},
  /*b*/  {000000,000000,000000,000000,0x007d,0x0083,0x0101,0x0101,0x0101,
          0x0101,0x0101,0x0083,0x007d,0x0001,0x0001,0x0001,0x0001},
  /*c*/  {000000,000000,000000,000000,0x007c,0x0082,0x0101,0x0001,0x0001,
          0x0001,0x0101,0x0082,0x007c,000000,000000,000000,000000},
  /*d*/  {000000,000000,000000,000000,0x017c,0x0182,0x0101,0x0101,0x0101,
          0x0101,0x0101,0x0182,0x017c,0x0100,0x0100,0x0100,0x0100},
  /*e*/  {000000,000000,000000,000000,0x007c,0x0002,0x0001,0x0001,0x01ff,
          0x0101,0x0101,0x0082,0x007c,000000,000000,000000,000000},
  /*f*/  {000000,000000,000000,000000,0x0010,0x0010,0x0010,0x0010,0x0010,
          0x0010,0x007c,0x0010,0x0010,0x0010,0x0110,0x00a0,0x0040},
  /*g*/  {0x007c,0x0082,0x0101,0x0100,0x017c,0x0182,0x0101,0x0101,0x0101,
          0x0101,0x0101,0x0182,0x017c,000000,000000,000000,000000},
  /*h*/  {000000,000000,000000,000000,0x0101,0x0101,0x0101,0x0101,0x0101,
          0x0103,0x0103,0x0085,0x0079,0x0001,0x0001,0x0001,0x0001},
  /*i*/  {000000,000000,000000,000000,0x007c,0x0010,0x0010,0x0010,0x0010,
          0x0010,0x0010,0x0018,000000,000000,0x0018,0x0018,000000},
  /*j*/  {0x003c,0x0042,0x0081,0x0080,0x0080,0x0080,0x0080,0x0080,0x0080,
          0x0080,0x0080,0x00c0,000000,000000,0x00c0,0x00c0,000000},
  /*k*/  {000000,000000,000000,000000,0x0082,0x0042,0x0022,0x0012,0x000a,
          0x0016,0x0022,0x0042,0x0002,0x0002,0x0002,0x0002,0x0002},
  /*l*/  {000000,000000,000000,000000,0x007c,0x0010,0x0010,0x0010,0x0010,
          0x0010,0x0010,0x0010,0x0010,0x0010,0x0010,0x0010,0x001c},
  /*m*/  {000000,000000,000000,000000,0x0111,0x0111,0x0111,0x0111,0x0111,
          0x0111,0x0111,0x00ab,0x0045,000000,000000,000000,000000},
  /*n*/  {000000,000000,000000,000000,0x0101,0x0101,0x0101,0x0101,0x0101,
          0x0101,0x0101,0x0083,0x007d,000000,000000,000000,000000},
  /*o*/  {000000,000000,000000,000000,0x007c,0x0082,0x0101,0x0101,0x0101,
          0x0101,0x0101,0x0082,0x007c,000000,000000,000000,000000},
  /*p*/  {0x0001,0x0001,0x0001,0x0001,0x007d,0x0003,0x0081,0x0101,0x0101,
          0x0101,0x0101,0x0083,0x007d,000000,000000,000000,000000},
  /*q*/  {0x0100,0x0100,0x0100,0x0100,0x017c,0x0182,0x0101,0x0101,0x0101,
          0x0101,0x0101,0x0182,0x017c,000000,000000,000000,000000},
  /*r*/  {000000,000000,000000,000000,0x0001,0x0001,0x0001,0x0001,0x0001,
          0x0001,0x0103,0x0085,0x0079,000000,000000,000000,000000},
  /*s*/  {000000,000000,000000,000000,0x007e,0x0081,0x0100,0x0080,0x007c,
          0x0002,0x0001,0x0102,0x00fc,000000,000000,000000,000000},
  /*t*/  {000000,000000,000000,000000,0x0040,0x00a0,0x0110,0x0010,0x0010,
          0x0010,0x0010,0x0010,0x00fe,0x0010,0x0010,0x0010,0x0010},
  /*u*/  {000000,000000,000000,000000,0x013c,0x0142,0x0181,0x0101,0x0101,
          0x0101,0x0101,0x0101,0x0101,000000,000000,000000,000000},
  /*v*/  {000000,000000,000000,000000,0x0010,0x0028,0x0044,0x0082,0x0101,
          0x0101,0x0101,0x0101,0x0101,000000,000000,000000,000000},
  /*w*/  {000000,000000,000000,000000,0x0044,0x00aa,0x0111,0x0111,0x0101,
          0x0101,0x0101,0x0101,0x0101,000000,000000,000000,000000},
  /*x*/  {000000,000000,000000,000000,0x0101,0x0082,0x0044,0x0028,0x0010,
          0x0028,0x0044,0x0082,0x0101,000000,000000,000000,000000},
  /*y*/  {0x007c,0x0082,0x0101,0x0100,0x0100,0x01fc,0x0102,0x0101,0x0101,
          0x0101,0x0101,0x0101,0x0101,000000,000000,000000,000000},
  /*z*/  {000000,000000,000000,000000,0x01ff,0x0002,0x0004,0x0008,0x0010,
          0x0020,0x0040,0x0080,0x01ff,000000,000000,000000,000000},
  /*{*/  {000000,000000,000000,000000,0x00c0,0x0020,0x0010,0x0010,0x0010,
          0x0008,0x0004,0x0008,0x0010,0x0010,0x0010,0x0020,0x00c0},
  /*|*/  {000000,000000,000000,000000,0x0010,0x0010,0x0010,0x0010,0x0010,
          000000,000000,000000,0x0010,0x0010,0x0010,0x0010,0x0010},
  /*}*/  {000000,000000,000000,000000,0x0006,0x0008,0x0010,0x0010,0x0010,
          0x0020,0x0040,0x0020,0x0010,0x0010,0x0010,0x0008,0x0006},
  /*~*/  {000000,000000,000000,000000,000000,000000,000000,000000,000000,
          000000,000000,000000,0x0040,0x00a0,0x0111,0x000a,0x0004},
  /*DEL*/{000000,000000,000000,000000,0x0155,000000,0x0155,000000,0x0155,
          000000,0x0155,000000,0x0155,000000,0x0155,000000,0x0155},
};

/* 13x25 font, bottom row first, left pixel in lsb */
const char_row fnt13x25[FNT_CHARS][FNT13X25_VBITS] = {
  /* */  {000000,000000,000000,000000,000000,000000,000000,000000,000000,
          000000,000000,000000,000000,000000,000000,000000,000000,000000,
          000000,000000,000000,000000,000000,000000,000000},
  /*!*/  {000000,000000,000000,000000,000000,0x00e0,0x00e0,0x00e0,000000,
          000000,000000,0x0040,0x0040,0x0040,0x0040,0x0040,0x00e0,0x00e0,
          0x00e0,0x00e0,0x00e0,0x00e0,0x00e0,0x00e0,0x0040},
  /*"*/  {000000,000000,000000,000000,000000,000000,000000,000000,000000,
          000000,000000,000000,000000,000000,000000,000000,000000,000000,
          0x0208,0x0208,0x0208,0x0208,0x0208,0x0208,0x0208},
  /*#*/  {000000,000000,000000,000000,000000,000000,0x0208,0x0208,0x0208,
          0x0208,0x0208,0x0208,0x1fff,0x0208,0x0208,0x0208,0x0208,0x0208,
          0x1fff,0x0208,0x0208,0x0208,0x0208,0x0208,0x0208},
  /*$*/  {000000,000000,000000,000000,000000,000000,0x0040,0x0040,0x0040,
          0x03f8,0x0444,0x0842,0x0840,0x0840,0x0440,0x03f8,0x0044,0x0042,
          0x0042,0x0842,0x0444,0x03f8,0x0040,0x0040,0x0040},
  /*%*/  {000000,000000,000000,000000,000000,000000,0x0c00,0x1200,0x1201,
          0x0c01,0x0002,0x0004,0x0008,0x0010,0x0020,0x0040,0x0080,0x0100,
          0x0200,0x0400,0x0800,0x1006,0x1009,0x0009,0x0006},
  /*&*/  {000000,000000,000000,000000,000000,000000,0x1078,0x1084,0x0902,
          0x0601,0x0601,0x0901,0x1081,0x0042,0x0024,0x0018,0x0018,0x0024,
          0x0042,0x0042,0x0042,0x0042,0x0024,0x0018,000000},
  /*'*/  {000000,000000,000000,000000,000000,000000,000000,000000,000000,
          000000,000000,000000,000000,000000,0x0001,0x0002,0x0004,0x0008,
          0x0010,0x0030,0x0078,0x0078,0x0078,0x0030,000000},
  /*(*/  {000000,000000,000000,000000,000000,000000,0x0080,0x0040,0x0020,
          0x0020,0x0010,0x0008,0x0008,0x0004,0x0004,0x0004,0x0004,0x0004,
          0x0008,0x0008,0x0010,0x0020,0x0020,0x0040,0x0080},
  /*)*/  {000000,000000,000000,000000,000000,000000,0x0020,0x0040,0x0080,
          0x0080,0x0100,0x0200,0x0200,0x0400,0x0400,0x0400,0x0400,0x0400,
          0x0200,0x0200,0x0100,0x0080,0x0080,0x0040,0x0020},
  /***/  {000000,000000,000000,000000,000000,000000,0x0040,0x0040,0x0040,
          0x1041,0x0842,0x0444,0x0248,0x0150,0x00e0,0x1fff,0x00e0,0x0150,
          0x0248,0x0444,0x0842,0x1041,0x0040,0x0040,0x0040},
  /*+*/  {000000,000000,000000,000000,000000,000000,000000,000000,000000,
          0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x1fff,0x0040,0x0040,
          0x0040,0x0040,0x0040,0x0040,000000,000000,000000},
  /*,*/  {000000,000000,0x0001,0x0002,0x0004,0x0008,0x0010,0x0030,0x0078,
          0x0078,0x0078,0x0030,000000,000000,000000,000000,000000,000000,
          000000,000000,000000,000000,000000,000000,000000},
  /*-*/  {000000,000000,000000,000000,000000,000000,000000,000000,000000,
          000000,000000,000000,000000,000000,000000,0x1fff,000000,000000,
          000000,000000,000000,000000,000000,000000,000000},
  /*.*/  {000000,000000,000000,000000,000000,000000,000000,0x0038,0x007c,
          0x007c,0x007c,0x0038,000000,000000,000000,000000,000000,000000,
          000000,000000,000000,000000,000000,000000,000000},
  /*-/-*/{000000,000000,000000,000000,000000,000000,000000,000000,0x0001,
          0x0001,0x0002,0x0004,0x0008,0x0010,0x0020,0x0040,0x0080,0x0100,
          0x0200,0x0400,0x0800,0x1000,0x1000,000000,000000},
  /*0*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0404,0x0802,
          0x1001,0x1003,0x1005,0x1009,0x1011,0x1021,0x1041,0x1081,0x1101,
          0x1201,0x1401,0x1801,0x1001,0x0802,0x0404,0x03f8},
  /*1*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0040,0x0040,
          0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,
          0x0040,0x0040,0x0040,0x0048,0x0070,0x0060,0x0040},
  /*2*/  {000000,000000,000000,000000,000000,000000,0x1fff,0x0001,0x0001,
          0x0001,0x0001,0x0001,0x0001,0x0002,0x03fc,0x0400,0x0800,0x1000,
          0x1000,0x1000,0x1000,0x1001,0x0802,0x0404,0x03f8},
  /*3*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0404,0x0802,
          0x1001,0x1000,0x1000,0x1000,0x0800,0x0400,0x03e0,0x0400,0x0800,
          0x1000,0x1000,0x1000,0x1001,0x0802,0x0404,0x03f8},
  /*4*/  {000000,000000,000000,000000,000000,000000,0x0200,0x0200,0x0200,
          0x0200,0x0200,0x0200,0x0200,0x1fff,0x0201,0x0201,0x0202,0x0204,
          0x0208,0x0210,0x0220,0x0240,0x0280,0x0300,0x0200},
  /*5*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0404,0x0802,
          0x1001,0x1000,0x1000,0x1000,0x1000,0x1000,0x0800,0x0400,0x03ff,
          0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x1fff},
  /*6*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0404,0x0802,
          0x1001,0x1001,0x1001,0x1001,0x0801,0x0401,0x03ff,0x0001,0x0001,
          0x0001,0x0001,0x0002,0x0004,0x0808,0x0410,0x03e0},
  /*7*/  {000000,000000,000000,000000,000000,000000,0x0001,0x0001,0x0001,
          0x0002,0x0002,0x0004,0x0008,0x0010,0x0020,0x0040,0x0080,0x0100,
          0x0200,0x0400,0x0800,0x0800,0x1000,0x1000,0x1fff},
  /*8*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0404,0x0802,
          0x1001,0x1001,0x1001,0x1001,0x0802,0x0404,0x03f8,0x0404,0x0802,
          0x1001,0x1001,0x1001,0x1001,0x0802,0x0404,0x03f8},
  /*9*/  {000000,000000,000000,000000,000000,000000,0x00f8,0x0104,0x0202,
          0x0400,0x0800,0x1000,0x1000,0x1000,0x1000,0x1ff8,0x1004,0x1002,
          0x1001,0x1001,0x1001,0x1001,0x0802,0x0404,0x03f8},
  /*:*/  {000000,000000,000000,000000,000000,000000,000000,000000,0x0030,
          0x0078,0x0078,0x0030,000000,000000,000000,000000,000000,000000,
          0x0030,0x0078,0x0078,0x0030,000000,000000,000000},
  /*;*/  {000000,000000,0x0001,0x0002,0x0004,0x0008,0x0010,0x0030,0x0078,
          0x0078,0x0078,0x0030,000000,000000,000000,000000,000000,000000,
          0x0030,0x0078,0x0078,0x0030,000000,000000,000000},
  /*<*/  {000000,000000,000000,000000,000000,000000,0x0200,0x0100,0x0080,
          0x0040,0x0020,0x0010,0x0008,0x0004,0x0002,0x0001,0x0002,0x0004,
          0x0008,0x0010,0x0020,0x0040,0x0080,0x0100,0x0200},
  /*=*/  {000000,000000,000000,000000,000000,000000,000000,000000,000000,
          000000,000000,000000,0x1fff,000000,000000,000000,000000,000000,
          0x1fff,000000,000000,000000,000000,000000,000000},
  /*>*/  {000000,000000,000000,000000,000000,000000,0x0008,0x0010,0x0020,
          0x0040,0x0080,0x0100,0x0200,0x0400,0x0800,0x1000,0x0800,0x0400,
          0x0200,0x0100,0x0080,0x0040,0x0020,0x0010,0x0008},
  /*?*/  {000000,000000,000000,000000,000000,0x0040,0x00e0,0x0040,000000,
          000000,000000,0x0040,0x0040,0x0080,0x0100,0x0200,0x0400,0x0800,
          0x1000,0x1000,0x1001,0x1001,0x0802,0x0404,0x03f8},
  /*@*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0404,0x0002,
          0x0001,0x0001,0x0ee1,0x1111,0x1111,0x1111,0x1111,0x1111,0x12e1,
          0x1001,0x1001,0x1001,0x1001,0x0802,0x0404,0x03f8},
  /*A*/  {000000,000000,000000,000000,000000,000000,0x1001,0x1001,0x1001,
          0x1001,0x1001,0x1001,0x1fff,0x1001,0x1001,0x1001,0x1001,0x0802,
          0x0802,0x0404,0x0208,0x0110,0x00a0,0x00a0,0x0040},
  /*B*/  {000000,000000,000000,000000,000000,000000,0x03ff,0x0408,0x0808,
          0x1008,0x1008,0x1008,0x1008,0x0808,0x0408,0x03f8,0x0408,0x0808,
          0x1008,0x1008,0x1008,0x1008,0x0808,0x0408,0x03ff},
  /*C*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0404,0x0802,
          0x1001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,
          0x0001,0x0001,0x0001,0x1001,0x0802,0x0404,0x03f8},
  /*D*/  {000000,000000,000000,000000,000000,000000,0x03ff,0x0408,0x0808,
          0x1008,0x1008,0x1008,0x1008,0x1008,0x1008,0x1008,0x1008,0x1008,
          0x1008,0x1008,0x1008,0x1008,0x0808,0x0408,0x03ff},
  /*E*/  {000000,000000,000000,000000,000000,000000,0x1fff,0x0001,0x0001,
          0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x007f,0x0001,0x0001,
          0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x1fff},
  /*F*/  {000000,000000,000000,000000,000000,000000,0x0001,0x0001,0x0001,
          0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x007f,0x0001,0x0001,
          0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x1fff},
  /*G*/  {000000,000000,000000,000000,000000,000000,0x0ff8,0x1004,0x1002,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1f01,0x0001,0x0001,
          0x0001,0x0001,0x0001,0x0001,0x0002,0x1004,0x0ff8},
  /*H*/  {000000,000000,000000,000000,000000,000000,0x1001,0x1001,0x1001,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1fff,0x1001,0x1001,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001},
  /*I*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0040,0x0040,
          0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,
          0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x03f8},
  /*J*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0404,0x0802,
          0x1001,0x1000,0x1000,0x1000,0x1000,0x1000,0x1000,0x1000,0x1000,
          0x1000,0x1000,0x1000,0x1000,0x1000,0x1000,0x1e00},
  /*K*/  {000000,000000,000000,000000,000000,000000,0x1001,0x0801,0x0401,
          0x0201,0x0101,0x0081,0x0041,0x0021,0x0011,0x000f,0x0009,0x0011,
          0x0021,0x0041,0x0081,0x0101,0x0201,0x0401,0x0801},
  /*L*/  {000000,000000,000000,000000,000000,000000,0x1fff,0x1001,0x1001,
          0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,
          0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001},
  /*M*/  {000000,000000,000000,000000,000000,000000,0x1001,0x1001,0x1001,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1041,0x1041,0x10a1,
          0x10a1,0x1111,0x1209,0x1209,0x1405,0x1803,0x1001},
  /*N*/  {000000,000000,000000,000000,000000,000000,0x1001,0x1001,0x1001,
          0x1001,0x1801,0x1401,0x1201,0x1201,0x1101,0x1081,0x1041,0x1041,
          0x1021,0x1011,0x1009,0x1009,0x1005,0x1003,0x1001},
  /*O*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0404,0x0802,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,
          0x1001,0x1001,0x1001,0x1001,0x0802,0x0404,0x03f8},
  /*P*/  {000000,000000,000000,000000,000000,000000,0x0001,0x0001,0x0001,
          0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x03ff,0x0401,0x0801,
          0x1001,0x1001,0x1001,0x1001,0x0801,0x0401,0x03ff},
  /*Q*/  {000000,000000,000000,000000,0x0c00,0x0200,0x03f8,0x0494,0x0862,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,
          0x1001,0x1001,0x1001,0x1001,0x0802,0x0404,0x03f8},
  /*R*/  {000000,000000,000000,000000,000000,000000,0x1001,0x0801,0x0401,
          0x0201,0x0101,0x0081,0x0041,0x0021,0x0011,0x03ff,0x0401,0x0801,
          0x1001,0x1001,0x1001,0x1001,0x0801,0x0401,0x03ff},
  /*S*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0404,0x0802,
          0x1001,0x1000,0x1000,0x1000,0x0800,0x0400,0x03f8,0x0004,0x0002,
          0x0001,0x0001,0x0001,0x1001,0x0802,0x0404,0x03f8},
  /*T*/  {000000,000000,000000,000000,000000,000000,0x00e0,0x0040,0x0040,
          0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,
          0x0040,0x0040,0x0040,0x0040,0x0040,0x1041,0x1fff},
  /*U*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0404,0x0802,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001},
  /*V*/  {000000,000000,000000,000000,000000,000000,0x0040,0x0040,0x00a0,
          0x00a0,0x0110,0x0110,0x0208,0x0208,0x0404,0x0404,0x0802,0x0802,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001},
  /*W*/  {000000,000000,000000,000000,000000,000000,0x1001,0x1001,0x1803,
          0x1405,0x1405,0x1209,0x1209,0x1111,0x1111,0x10a1,0x1041,0x1001,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001},
  /*X*/  {000000,000000,000000,000000,000000,000000,0x1001,0x1001,0x1001,
          0x0802,0x0802,0x0404,0x0208,0x0110,0x00a0,0x0040,0x00a0,0x0110,
          0x0208,0x0404,0x0802,0x0802,0x1001,0x1001,0x1001},
  /*Y*/  {000000,000000,000000,000000,000000,000000,0x0040,0x0040,0x0040,
          0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x00a0,0x0110,0x0208,
          0x0404,0x0802,0x0802,0x1001,0x1001,0x1001,0x1001},
  /*Z*/  {000000,000000,000000,000000,000000,000000,0x1fff,0x0001,0x0001,
          0x0002,0x0004,0x0004,0x0008,0x0010,0x0020,0x0040,0x0080,0x0100,
          0x0200,0x0400,0x0400,0x0800,0x1000,0x1000,0x1fff},
  /*[*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0008,0x0008,
          0x0008,0x0008,0x0008,0x0008,0x0008,0x0008,0x0008,0x0008,0x0008,
          0x0008,0x0008,0x0008,0x0008,0x0008,0x0008,0x03f8},
  /*\ */ {000000,000000,000000,000000,000000,000000,000000,000000,0x1000,
          0x1000,0x0800,0x0400,0x0200,0x0100,0x0080,0x0040,0x0020,0x0010,
          0x0008,0x0004,0x0002,0x0001,0x0001,000000,000000},
  /*]*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0200,0x0200,
          0x0200,0x0200,0x0200,0x0200,0x0200,0x0200,0x0200,0x0200,0x0200,
          0x0200,0x0200,0x0200,0x0200,0x0200,0x0200,0x03f8},
  /*^*/  {000000,000000,000000,000000,000000,000000,000000,000000,000000,
          000000,000000,000000,000000,000000,000000,000000,000000,000000,
          0x1001,0x0802,0x0404,0x0208,0x0110,0x00a0,0x0040},
  /*_*/  {000000,000000,000000,000000,000000,000000,0x1fff,000000,000000,
          000000,000000,000000,000000,000000,000000,000000,000000,000000,
          000000,000000,000000,000000,000000,000000,000000},
  /*`*/  {000000,000000,000000,000000,000000,000000,000000,000000,000000,
          000000,000000,000000,000000,000000,0x0400,0x0200,0x0100,0x0080,
          0x0040,0x0060,0x00f0,0x00f0,0x00f0,0x0060,000000},
  /*a*/  {000000,000000,000000,000000,000000,000000,0x17f8,0x0804,0x0802,
          0x0802,0x0802,0x0804,0x0ff8,0x0800,0x0800,0x0800,0x0800,0x0404,
          0x03f8,000000,000000,000000,000000,000000,000000},
  /*b*/  {000000,000000,000000,000000,000000,000000,0x03f9,0x0405,0x0803,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x0803,0x0405,
          0x03f9,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001},
  /*c*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0404,0x0802,
          0x1001,0x0001,0x0001,0x0001,0x0001,0x0001,0x1001,0x0802,0x0404,
          0x03f8,000000,000000,000000,000000,000000,000000},
  /*d*/  {000000,000000,000000,000000,000000,000000,0x13f8,0x1404,0x1802,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1802,0x1404,
          0x13f8,0x1000,0x1000,0x1000,0x1000,0x1000,0x1000},
  /*e*/  {000000,000000,000000,000000,000000,000000,0x0ff8,0x0004,0x0002,
          0x0001,0x0001,0x0001,0x1fff,0x1001,0x1001,0x1001,0x0802,0x0404,
          0x03f8,000000,000000,000000,000000,000000,000000},
  /*f*/  {000000,000000,000000,000000,000000,000000,0x0040,0x0040,0x0040,
          0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x03f8,0x0040,0x0040,
          0x0040,0x0040,0x0040,0x1040,0x0880,0x0500,0x0200},
  /*g*/  {0x03f8,0x0404,0x0802,0x1001,0x1000,0x1000,0x13f8,0x1404,0x1802,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1802,0x1404,
          0x13f8,000000,000000,000000,000000,000000,000000},
  /*h*/  {000000,000000,000000,000000,000000,000000,0x1001,0x1001,0x1001,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x0803,0x0405,
          0x03f9,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001},
  /*i*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0040,0x0040,
          0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0070,
          000000,000000,000000,0x00e0,0x00e0,0x00e0,000000},
  /*j*/  {0x00f0,0x0108,0x0204,0x0402,0x0400,0x0400,0x0400,0x0400,0x0400,
          0x0400,0x0400,0x0400,0x0400,0x0400,0x0400,0x0400,0x0400,0x0700,
          000000,000000,000000,0x0700,0x0700,0x0700,000000},
  /*k*/  {000000,000000,000000,000000,000000,000000,0x0804,0x0404,0x0204,
          0x0104,0x0084,0x0044,0x0024,0x0014,0x002c,0x0044,0x0084,0x0104,
          0x0204,0x0004,0x0004,0x0004,0x0004,0x0004,0x0004},
  /*l*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0040,0x0040,
          0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,
          0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0070},
  /*m*/  {000000,000000,000000,000000,000000,000000,0x1041,0x1041,0x1041,
          0x1041,0x1041,0x1041,0x1041,0x1041,0x1041,0x1041,0x08a3,0x0515,
          0x0209,000000,000000,000000,000000,000000,000000},
  /*n*/  {000000,000000,000000,000000,000000,000000,0x1001,0x1001,0x1001,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x0803,0x0405,
          0x03f9,000000,000000,000000,000000,000000,000000},
  /*o*/  {000000,000000,000000,000000,000000,000000,0x03f8,0x0404,0x0802,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x0802,0x0404,
          0x03f8,000000,000000,000000,000000,000000,000000},
  /*p*/  {0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x03f9,0x0405,0x0803,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x0803,0x0405,
          0x03f9,000000,000000,000000,000000,000000,000000},
  /*q*/  {0x1000,0x1000,0x1000,0x1000,0x1000,0x1000,0x13f8,0x1404,0x1802,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1802,0x1404,
          0x13f8,000000,000000,000000,000000,000000,000000},
  /*r*/  {000000,000000,000000,000000,000000,000000,0x0001,0x0001,0x0001,
          0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x1001,0x0803,0x0405,
          0x03f9,000000,000000,000000,000000,000000,000000},
  /*s*/  {000000,000000,000000,000000,000000,000000,0x03fc,0x0402,0x0800,
          0x0800,0x0800,0x0400,0x03f8,0x0004,0x0002,0x0002,0x0002,0x0804,
          0x07f8,000000,000000,000000,000000,000000,000000},
  /*t*/  {000000,000000,000000,000000,000000,000000,0x0200,0x0500,0x0880,
          0x1040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,
          0x07fc,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040},
  /*u*/  {000000,000000,000000,000000,000000,000000,0x13f8,0x1404,0x1802,
          0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,
          0x1001,000000,000000,000000,000000,000000,000000},
  /*v*/  {000000,000000,000000,000000,000000,000000,0x0040,0x00a0,0x0110,
          0x0208,0x0404,0x0802,0x0802,0x1001,0x1001,0x1001,0x1001,0x1001,
          0x1001,000000,000000,000000,000000,000000,000000},
  /*w*/  {000000,000000,000000,000000,000000,000000,0x0208,0x0514,0x08a2,
          0x08a2,0x1041,0x1041,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,
          0x1001,000000,000000,000000,000000,000000,000000},
  /*x*/  {000000,000000,000000,000000,000000,000000,0x1001,0x0802,0x0404,
          0x0208,0x0110,0x00a0,0x0040,0x00a0,0x0110,0x0208,0x0404,0x0802,
          0x1001,000000,000000,000000,000000,000000,000000},
  /*y*/  {0x03f8,0x0404,0x0802,0x1001,0x1000,0x1000,0x1000,0x1000,0x1ff8,
          0x1004,0x1002,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,0x1001,
          0x1001,000000,000000,000000,000000,000000,000000},
  /*z*/  {000000,000000,000000,000000,000000,000000,0x1fff,0x0002,0x0004,
          0x0008,0x0010,0x0020,0x0040,0x0080,0x0100,0x0200,0x0400,0x0800,
          0x1fff,000000,000000,000000,000000,000000,000000},
  /*{*/  {000000,000000,000000,000000,000000,000000,0x0600,0x0100,0x0080,
          0x0040,0x0040,0x0040,0x0040,0x0040,0x0020,0x0010,0x0020,0x0040,
          0x0040,0x0040,0x0040,0x0040,0x0080,0x0100,0x0600},
  /*|*/  {000000,000000,000000,000000,000000,000000,0x0040,0x0040,0x0040,
          0x0040,0x0040,0x0040,0x0040,000000,000000,000000,000000,000000,
          0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040},
  /*}*/  {000000,000000,000000,000000,000000,000000,0x000c,0x0010,0x0020,
          0x0040,0x0040,0x0040,0x0040,0x0040,0x0080,0x0100,0x0080,0x0040,
          0x0040,0x0040,0x0040,0x0040,0x0020,0x0010,0x000c},
  /*~*/  {000000,000000,000000,000000,000000,000000,000000,000000,000000,
          000000,000000,000000,000000,000000,000000,000000,000000,000000,
          0x0600,0x0900,0x1080,0x1041,0x0021,0x0012,0x000c},
  /*DEL*/{000000,000000,000000,000000,000000,000000,0x1249,000000,000000,
          0x1249,000000,000000,0x1249,000000,000000,0x1249,000000,000000,
          0x1249,000000,000000,0x1249,000000,000000,0x1249},
};


/*
   ** The plotting area is defined as a huge bitmap.
   ** The bitmap is stored in a dynamically allocated pixel array b_p
   **
   ** The bitmap is allocated (and initialized to zero) with
   ** b_makebitmap(xsize, ysize, planes)
   ** and freed with b_freebitmap()
   ** xsize and ysize will be rounded up to a multiple of 8.
   **
   ** Valid (int) coordinates range from zero to (xsize-1,ysize-1)
   **
   ** Plotting is done via b_move(x, y) and b_vector(x, y) functions,
   ** where the point (x,y) is the target to go from the current point
   ** To set the color use b_setvalue(value) where value is the value
   ** (0 or 1 or a color number) to be stored in every pixel.
   ** To get dotted line styles, use b_setlinetype(linetype).
   **
   ** Internally all plotting goes through b_setpixel(x, y, value).
 */


/* bitmaps used for filled boxes style (ULIG) */

#define fill_bitmap_width 8
#define fill_bitmap_height 8

#define fill_halftone_num 5
static unsigned char fill_halftone_bitmaps[fill_halftone_num][8] ={
  { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },   /* no fill */
  { 0x11, 0x44, 0x11, 0x44, 0x11, 0x44, 0x11, 0x44 },   /* 25% pattern */
  { 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa },   /* 50% pattern */
  { 0x77, 0xdd, 0x77, 0xdd, 0x77, 0xdd, 0x77, 0xdd },   /* 75% pattern */
  { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }    /* solid pattern */
};

#define fill_pattern_num 8
static unsigned char fill_pattern_bitmaps[fill_pattern_num][8] ={
    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } /* no fill */
   ,{ 0x82, 0x44, 0x28, 0x10, 0x28, 0x44, 0x82, 0x01 } /* cross-hatch      (1) */
   ,{ 0x88, 0x55, 0x22, 0x55, 0x88, 0x55, 0x22, 0x55 } /* double crosshatch(2) */
   ,{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } /* solid fill       (3) */
   ,{ 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 } /* diagonal stripes (4) */
   ,{ 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 } /* diagonal stripes (5) */
   ,{ 0x11, 0x11, 0x22, 0x22, 0x44, 0x44, 0x88, 0x88 } /* diagonal stripes (6) */
   ,{ 0x88, 0x88, 0x44, 0x44, 0x22, 0x22, 0x11, 0x11 } /* diagonal stripes (7) */
#if (0)
   ,{ 0x03, 0x0C, 0x30, 0xC0, 0x03, 0x0C, 0x30, 0xC0 } /* diagonal stripes (8) */
   ,{ 0xC0, 0x30, 0x0C, 0x03, 0xC0, 0x30, 0x0C, 0x03 } /* diagonal stripes (9) */
#endif
};

/*
 * set pixel (x, y, value) to value value (this can be 1/0 or a color number).
 */
static GP_INLINE void
b_setpixel(unsigned int x, unsigned int y, unsigned int value)
{
    unsigned int row;
    unsigned char mask;
    unsigned int i;

    if (b_rastermode) {
	/* interchange so that new (x,y) is old (y,b_ysize-1-x) */
	row = x;		/* temp storage */
	x = y;
	y = b_ysize - 1 - row;
    }
    if ((x < b_xsize) && (y < b_ysize)) {
	row = y / 8;
	mask = 1 << (y % 8);

	for (i = 0; i < b_planes; i++) {
	    if (value & 1)
		*((*b_p)[row] + x) |= mask;
	    else
		*((*b_p)[row] + x) &= ~mask;
	    row += b_psize;
	    value >>= 1;
	}
    }
#ifdef BITMAPDEBUG
    else {
	if (b_rastermode)
	    fprintf(stderr, "Warning: setpixel(%d, %d, %d) out of bounds\n",
		    b_ysize - 1 - y, x, value);
	else
	    fprintf(stderr, "Warning: setpixel(%d, %d, %d) out of bounds\n",
		    x, y, value);
    }
#endif
}


/*
 * set pixel (x, y) to the set value
 */
void
b_putpixel(unsigned int x, unsigned int y)
{
    b_setpixel(x, y, b_value);
}


/*
 * get pixel (x,y) value
 */
unsigned int
b_getpixel(unsigned int x, unsigned int y)
{
    unsigned int row;
    unsigned char mask;
    unsigned int value=0; /* HBB 991123: initialize! */
    int i;

    if (b_rastermode) {
	row = x;
	x = y;
	y = b_ysize-1-row;
    }
    if ((x < b_xsize) && (y < b_ysize)) {
	row = y/8 + (b_planes-1)*b_psize;
	mask = 1<<(y%8);

	for (i=0; i<b_planes; i++) {
	    if ( *((*b_p)[row]+x) & mask )
		value |= 1;
	    row -= b_psize;
	    value <<= 1;
	}

	/* HBB 991123: the missing '>>1' was the 'every second color' problem
	 * with PNG in 3.8a...*/
	return(value>>1);
    } else {
#ifdef BITMAPDEBUG
	if (b_rastermode)
	    fprintf(stderr, "Warning: getpixel(%d,%d) out of bounds\n",
		    b_ysize-1-y, x);
	else
	    fprintf(stderr, "Warning: getpixel(%d,%d) out of bounds\n", x, y);
#endif
	return(0);
    }
}


/*
 * allocate the bitmap
 */
void
b_makebitmap(unsigned int x, unsigned int y, unsigned int planes)
{
    unsigned int j;
    unsigned int rows;

    x = 8 * ((x + 7) / 8);	/* round up to multiple of 8 */
    y = 8 * ((y + 7) / 8);	/* round up to multiple of 8 */
    b_psize = y / 8;		/* size of each plane */
    rows = b_psize * planes;	/* total number of rows of 8 pixels high */
    b_xsize = x;
    b_ysize = y;
    b_currx = b_curry = 0;
    b_planes = planes;
    b_value = 1;
    b_angle = 0;
    b_rastermode = 0;
    /* allocate row pointers */
    b_p = (bitmap *) gp_alloc(rows * sizeof(pixels *), "bitmap row buffer");
    memset(b_p, 0, rows * sizeof(pixels *));
    for (j = 0; j < rows; j++) {
	/* allocate bitmap buffers */
	(*b_p)[j] = (pixels *) gp_alloc(x * sizeof(pixels), (char *) NULL);
	if ((*b_p)[j] == (pixels *) NULL) {
	    b_freebitmap();	/* free what we have already allocated */
	    int_error(NO_CARET, "out of memory for bitmap buffer");
	}
	memset((*b_p)[j], 0, x * sizeof(pixels));
    }
}


/*
 * free the allocated bitmap
 */
void
b_freebitmap()
{
    unsigned int j, rows;

    rows = b_psize * b_planes;	/* total number of rows of 8 pixels high */
    for (j = 0; j < rows; j++) {
	(void) free((char *) (*b_p)[j]);
    }
    (void) free((char *) b_p);
    b_p = (bitmap *) (NULL);
}


/*
 * set pixel at (x,y) with color b_value and dotted mask b_linemask.
 */
static GP_INLINE void
b_setmaskpixel(unsigned int x, unsigned int y, unsigned int value)
{
    /* dotted line generator */
    if ((b_linemask >> b_maskcount) & (unsigned int) (1)) {
	b_setpixel(x, y, value);
    }
    b_maskcount = (b_maskcount + 1) % 16;
    b_lastx = x;		/* last pixel set with mask */
    b_lasty = y;
}


/*
 * draw a line from (x1,y1) to (x2,y2)
 * with color b_value and dotted mask b_linemask.
 */
static void
b_line(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2)
{
    int runcount;
    int dx, dy;
    int xinc, yinc;
    unsigned int xplot, yplot;

    runcount = 0;
    dx = abs((int) (x1) - (int) (x2));
    if (x2 > x1)
	xinc = 1;
    else if (x2 == x1)
	xinc = 0;
    else
	xinc = -1;
    dy = abs((int) (y1) - (int) (y2));
    if (y2 > y1)
	yinc = 1;
    else if (y2 == y1)
	yinc = 0;
    else
	yinc = -1;
    xplot = x1;
    yplot = y1;
    if (dx > dy) {
	/* iterate x */
	if ((b_linemask == 0xffff) ||
	    ((xplot != b_lastx) && (yplot != b_lasty)))
	    b_setmaskpixel(xplot, yplot, b_value);
	while (xplot != x2) {
	    xplot += xinc;
	    runcount += dy;
	    if (runcount >= (dx - runcount)) {
		yplot += yinc;
		runcount -= dx;
	    }
	    b_setmaskpixel(xplot, yplot, b_value);
	}
    } else {
	/* iterate y */
	if ((b_linemask == 0xffff) ||
	    ((xplot != b_lastx) && (yplot != b_lasty)))
	    b_setmaskpixel(xplot, yplot, b_value);
	while (yplot != y2) {
	    yplot += yinc;
	    runcount += dx;
	    if (runcount >= (dy - runcount)) {
		xplot += xinc;
		runcount -= dy;
	    }
	    b_setmaskpixel(xplot, yplot, b_value);
	}
    }
}


/* draw a wide line (solid only, no dash pattern */
/* code from sixeltek terminal by Erik Olofsen */
static void
b_wline(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2)
{
    int runcount;
    int dx, dy;
    int xinc, yinc;
    unsigned int xplot, yplot;
    unsigned int i, wh, wh2;
    int x;

    if (b_lw <= 1.) {
	b_line(x1, y1, x2, y2);
	return;
    }

    wh = (int) (b_lw + 0.5);
    wh2 = wh / 2;

    /* algorithm from bitmap.c */
    runcount = 0;
    dx = abs((int) (x1) - (int) (x2));
    if (x2 > x1)
	xinc = 1;
    else if (x2 == x1)
	xinc = 0;
    else
	xinc = -1;
    dy = abs((int) (y1) - (int) (y2));
    if (y2 > y1)
	yinc = 1;
    else if (y2 == y1)
	yinc = 0;
    else
	yinc = -1;
    xplot = x1;
    yplot = y1;
    if (dx > dy) {
	/* iterate x */
	while (xplot != x2) {
	    xplot += xinc;
	    runcount += dy;
	    if (runcount >= (dx - runcount)) {
		yplot += yinc;
		runcount -= dx;
	    }
	    for (i = 1; i <= wh; i++) {
		x = xplot - wh2;
		/* for (x = (int) xplot - (int) wh2; dx <= ((int) xplot - (int) wh2 + wh); x++) */
		   b_setpixel(x, yplot - wh2 + i, b_value);
	    }
	}
    } else {
	/* iterate y */
	while (yplot != y2) {
	    yplot += yinc;
	    runcount += dx;
	    if (runcount >= (dy - runcount)) {
		xplot += xinc;
		runcount -= dy;
	    }
	    for (i = 1; i <= wh; i++) {
		x = xplot - wh2;
		/* for (x = (int) xplot - (int) wh2; dx <= ((int) xplot - (int) wh2 + wh); x++) */
		   b_setpixel(x, yplot - wh2 + i, b_value);
	    }
	}
    }
}


/*
 * set character size
 */
void
b_charsize(unsigned int size)
{
    int j;
    switch (size) {
    case FNT5X9:
	b_hchar = FNT5X9_HCHAR;
	b_hbits = FNT5X9_HBITS;
	b_vchar = FNT5X9_VCHAR;
	b_vbits = FNT5X9_VBITS;
	for (j = 0; j < FNT_CHARS; j++)
	    b_font[j] = fnt5x9[j];
	break;
    case FNT9X17:
	b_hchar = FNT9X17_HCHAR;
	b_hbits = FNT9X17_HBITS;
	b_vchar = FNT9X17_VCHAR;
	b_vbits = FNT9X17_VBITS;
	for (j = 0; j < FNT_CHARS; j++)
	    b_font[j] = fnt9x17[j];
	break;
    case FNT13X25:
	b_hchar = FNT13X25_HCHAR;
	b_hbits = FNT13X25_HBITS;
	b_vchar = FNT13X25_VCHAR;
	b_vbits = FNT13X25_VBITS;
	for (j = 0; j < FNT_CHARS; j++)
	    b_font[j] = fnt13x25[j];
	break;
    default:
	int_error(NO_CARET, "Unknown character size");
    }
}


/*
 * set b_linemask to b_pattern[linetype]
 */
void
b_setlinetype(int linetype)
{
    if (linetype >= 7)
	linetype %= 7;
    if (linetype < LT_SOLID)
	linetype = LT_SOLID;

    b_linemask = b_pattern[linetype + 2];
    b_maskcount = 0;
}


void
b_dashtype(int type, t_dashtype *custom_dash_type)
{
    if (type >= 0)
	;
    else if (type == DASHTYPE_AXIS)
	type = LT_AXIS;
    else
	type = LT_SOLID; /* solid, also for custom dash types */
    b_setlinetype(type);
}


/*
 * set l_lw to linewidth
 */
void
b_linewidth(double linewidth)
{
    b_lw = linewidth;
}


/*
 * set b_value to value
 */
void
b_setvalue(unsigned int value)
{
    b_value = value;
}


/*
 * move to (x,y)
 */
void
b_move(unsigned int x, unsigned int y)
{
    b_currx = x;
    b_curry = y;
}


/*
 * draw to (x,y) with color b_value
 */
void
b_vector(unsigned int x, unsigned int y)
{
    /* We can't clip properly, but we can refuse to draw out of bounds */
    if (x < term->xmax && y < term->ymax
    &&  b_currx < term->xmax && b_curry < term->ymax)
	b_wline(b_currx, b_curry, x, y);
    b_currx = x;
    b_curry = y;
}


/*
 * put text str at (x,y) with color b_value and rotation b_angle
 */
void
b_put_text(unsigned int x0, unsigned int y0, const char *str)
{
    int i, j, k;
    float sa, ca, x, y;
    char_row fc;
    size_t l;

    ca = cos(DEG2RAD * b_angle);
    sa = sin(DEG2RAD * b_angle);

    x = 0.5f + x0;
    y = 0.5f + y0;

    switch (b_justify) {
    case LEFT:
	x += sa * b_vchar / 2.f;
	y -= ca * b_vchar / 2.f;
	break;
    case CENTRE:
	l = strlen(str);
	x += sa * b_vchar / 2.f - ca * l * b_hchar / 2;
	y -= ca * b_vchar / 2.f + sa * l * b_hchar / 2;
	break;
    case RIGHT:
	l = strlen(str);
	x += sa * b_vchar / 2.f - ca * l * b_hchar;
	y -= ca * b_vchar / 2.f + sa * l * b_hchar;
	break;
    }

    k = 0;
    while (str[k]) {
	char c = GPMAX(str[k], ' ') - ' ';
	if (c >= FNT_CHARS) c = 0;
	for (i = 0; i < b_vbits; i++) {
	    fc = b_font[(unsigned char)c][i];
	    if (c == '_' - ' ') {	/* treat underline specially */
		if (fc) {		/* this this the underline row ? */
		    /* draw the under line for the full h_char width */
		    for (j = 0; j < b_hchar; j++)
			b_setpixel(x + ca * j - sa * i, y + ca * i + sa * j, b_value);
		}
	    } else {
		for (j = 0; j < b_hbits; j++) {
		    if ((fc >> j) & 1)
			b_setpixel(x + ca * j - sa * i, y + ca * i + sa * j, b_value);
		}
	    }
	}
	k++;
	x += ca * b_hchar;
	y += sa * b_hchar;
    }
}


int
b_text_angle(float ang)
{
    b_angle = ang;
    return TRUE;
}


int
b_justify_text(enum JUSTIFY mode)
{
    b_justify = mode;
    return 1;
}


/* New function by ULIG */
void
b_boxfill(
    int style,
    unsigned int x, unsigned int y,
    unsigned int w, unsigned int h)
{
    unsigned int ix, iy;
    int pixcolor, pat, mask, idx, bitoffs;
    unsigned char *fillbitmap;
    TBOOLEAN transparent = FALSE;

    switch (style & 0xf) {
    case FS_TRANSPARENT_SOLID:
	transparent = TRUE;
	/* fall-through */
    case FS_SOLID:
	/* use halftone fill pattern according to filldensity */
	/* filldensity is from 0..100 percent */
	idx = (int) (((style >> 4) * (fill_halftone_num - 1) + 50) / 100 );
	if (idx < 0)
	    idx = 0;
	if (idx >= fill_halftone_num)
	    idx = fill_halftone_num - 1;
	fillbitmap = fill_halftone_bitmaps[idx];
	pixcolor = b_value;
	break;
    case FS_TRANSPARENT_PATTERN:
	transparent = TRUE;
	/* fall-through */
    case FS_PATTERN:
	/* use fill pattern according to fillpattern */
	idx = (style >> 4);  /* fillpattern is enumerated */
	if (idx < 0)
	    idx = 0;
	idx %= fill_pattern_num;
	fillbitmap = fill_pattern_bitmaps[idx];
	pixcolor = b_value;
	break;
    case FS_DEFAULT:
	/* Fill with current color, wherever it came from */
	fillbitmap = fill_halftone_bitmaps[fill_halftone_num - 1];
	pixcolor = b_value;
	break;
    case FS_EMPTY:
    default:
	/* fill with background color */
	fillbitmap = fill_halftone_bitmaps[0];
	pixcolor = 0;
    }

    /* this implements a primitive raster generator, which plots the */
    /* bitmaps point by point calling b_setpixel(). Perhaps someone */
    /* will implement a more efficient solution */
    for (iy = y; iy < y + h; iy++) { /* box height */
	bitoffs = iy % fill_bitmap_width;
	pat = fillbitmap[bitoffs];
	for (ix = x; ix < x + w; ix++) { /* box width */
	    mask = 1 << (ix % fill_bitmap_width);
	    /* actual pixel = 0 or color, according to pattern */
	    if (pat & mask)
		b_setpixel(ix, iy, pixcolor);
	    else if (!transparent)
		b_setpixel(ix, iy, 0);
	}
    }

}

/* code from sixeltek terminal by Erik Olofsen */
void
b_filled_polygon(int points, gpiPoint *corners)
{
    int nodes, *nodex, px, py, i, j, swap;
    int style = corners->style;
    int pixcolor, pat, mask, idx, bitoffs;
    unsigned char *fillbitmap;
    TBOOLEAN transparent = FALSE;

    switch (style & 0xf) {
    case FS_TRANSPARENT_SOLID:
	transparent = TRUE;
	/* fall-through */
    case FS_SOLID:
	/* use halftone fill pattern according to filldensity */
	/* filldensity is from 0..100 percent */
	idx = GPMAX((int) (((style >> 4) * (fill_halftone_num - 1) + 50) / 100), 0);
	if (idx >= fill_halftone_num)
	    idx = fill_halftone_num - 1;
	fillbitmap = fill_halftone_bitmaps[idx];
	pixcolor = b_value;
	break;
    case FS_TRANSPARENT_PATTERN:
	transparent = TRUE;
	/* fall-through */
    case FS_PATTERN:
	/* use fill pattern according to fillpattern */
	idx = GPMAX((style >> 4), 0);  /* fillpattern is enumerated */
	idx %= fill_pattern_num;
	fillbitmap = fill_pattern_bitmaps[idx];
	pixcolor = b_value;
	break;
    case FS_DEFAULT:
	/* Fill with current color, wherever it came from */
	fillbitmap = fill_halftone_bitmaps[fill_halftone_num - 1];
	pixcolor = b_value;
	break;
    case FS_EMPTY:
    default:
	/* fill with background color */
	fillbitmap = fill_halftone_bitmaps[0];
	pixcolor = 0;
    }

    nodex = (int *) malloc(sizeof(int) * points);

    for (py = 0; py < b_ysize; py++) {
	bitoffs = py % fill_bitmap_width;
	pat = fillbitmap[bitoffs];

	nodes = 0;
	j = points - 1;
	for (i = 0; i < points; i++) {
	    if ((corners[i].y < py && corners[j].y >= py) ||
		(corners[j].y < py && corners[i].y >= py)) {
		nodex[nodes++] = (int) ((corners[i].x + (double)(py - corners[i].y) /
					  (double)(corners[j].y - corners[i].y) *
					  (corners[j].x - corners[i].x)) + 0.5);
	    }
	    j = i;
	}

	i = 0;
	while (i < nodes - 1) {
	    if (nodex[i] > nodex[i + 1]) {
		swap = nodex[i];
		nodex[i] = nodex[i + 1];
		nodex[i + 1] = swap;
		if (i) i--;
	    } else {
		i++;
	    }
	}

	for (i = 0; i < nodes; i += 2) {
	    for (px = nodex[i]; px < nodex[i + 1]; px++) {
		mask = 1 << (px % fill_bitmap_width);
		/* actual pixel = 0 or color, according to pattern */
		if (pat & mask)
		    b_setpixel(px, py, pixcolor);
		else if (!transparent)
		    b_setpixel(px, py, 0);
	    }
	}
    }

    free(nodex);
}
